//
//  YouHuiQuanViewController.m
//  XDBaseFrameWork
//
//  Created by xindao on 14-10-2.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "YouHuiQuanViewController.h"
#import "YouHuiQuanCell.h"
@interface YouHuiQuanViewController ()

@end

@implementation YouHuiQuanViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.titleLabel.text = @"优惠券";

    dataArray = [[NSMutableArray alloc] init];

    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT-UI_NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:myTableView];
    
    noDataIV = [[UIImageView alloc] initWithFrame:CGRectMake((UI_SCREEN_WIDTH-111)/2, (UI_MAINSCREEN_HEIGHT - UI_NAVIGATION_BAR_HEIGHT - 78)/2, 111, 78)];
    noDataIV.image = [UIImage imageNamed:@"noData"];
    [self.contentView addSubview:noDataIV];
    noDataIV.hidden = YES;

    [self getDataWithDict:@{@"CustomerID":[[NSUserDefaults standardUserDefaults] objectForKey:kMY_USER_ID] } api:API_GETYOUHUIQUAN];
}

-(void)getDataWithDict:(NSDictionary *)dict api:(NSString *)api
{
    if (![XDTools NetworkReachable]) {
        [XDTools showTips:brokenNetwork toView:self.contentView];
        return;
    }
    
    noDataIV.hidden = YES;
    
    __weak ASIHTTPRequest *request = [XDTools postRequestWithDict:dict API:api];
    [request setCompletionBlock:^{
        
        [XDTools hideProgress:self.contentView];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        
        DDLOG(@"temdic = %@",tempDic);
        
        if([[tempDic objectForKey:@"status"]intValue] == 1){
            dataArray = tempDic[@"data"];
            if (dataArray.count) {
                noDataIV.hidden = YES;
            }else{
                noDataIV.hidden = NO;
            }
            
            //记录优惠券的数量
            NSString * currentNum = [NSString stringWithFormat:@"%d",dataArray.count];
            [[NSUserDefaults standardUserDefaults]setObject:currentNum forKey:@"VoucherNum"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            
            [myTableView reloadData];
        }else{
            [XDTools showTips:tempDic[@"msg"] toView:self.contentView];
        }
        
    }];
    
    [request setFailedBlock:^{
        [XDTools hideProgress:self.contentView];
    }];
    
    [request startAsynchronous];
    [XDTools showProgress:self.contentView];
}


#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 112;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * cellIde = @"cell";
    YouHuiQuanCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIde];
    if (!cell) {
        cell = [[YouHuiQuanCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIde];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary * dic = dataArray[indexPath.row];
    
    [cell initViewsWithDict:dic];

    return cell;
    
}

#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_delegate) {
        int status = [dataArray[indexPath.row][@"status"] intValue];
        if (!status) {
            [XDTools showTips:@"亲，该优惠券已经过期了" toView:self.contentView];
            return;
        }else if (status == 1) {
            [XDTools showTips:@"亲，该优惠券已经使用过了" toView:self.contentView];
            return;
        }
        [_delegate willUseYouHuiQuan:dataArray[indexPath.row]];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
